<template>
	<view>
		汇总记录：<br/>
		<textarea v-model="oldData" maxlength="100000">
			
		</textarea>
		处理后:<br/>
		<textarea v-model="newData" maxlength="100000">
			
		</textarea>
		<button @click="dealDev">
			处理设备
		</button>
		
	</view>
</template>

<script>
	import axios from 'axios';
	import tableData from './tableData.js'
	import * as XLSX from 'xlsx';
	import {
		saveAs
	} from 'file-saver';
	export default {
		data() {
			return {
				oldData:'',
				newData:'',
				peopleArrOld:[],
				peopleArrNew:[],
				peopleArr:[
					'李钦章',
					'陈锡漫',
					'陈志威',
					'董梓浩',
					'梁伟均',
					'李波源',
					'黎富恒',
					'李金良',
					'黎伟锋',
					'李晓通',
					'罗星威',
					'马明锋',
					'莫本慰',
					'莫志杰',
					'覃基发',
					'吴烨希',
					'吴永健',
					'肖光',
					'谢颖通',
					'赵敏羽',
					'赵伟锋',
					'郑军平',
					'朱楷城',
					'邹仲强'
				],
				deviceArrOld:[],
				deviceArrNew:[],
				deviceArr:[
				],
				textContent:'',
				//负责人
				deviceMasterArr:[
				],
				textContentMaster:'',
				//汇总数据
				deviceGroupByArr:[],
				
			}
		},
		onLoad() {
			
		},
		created() {
			
		},
		computed: {
	
		},
		methods: {
			
			dealDev(){
				//设备
				this.appendDev();
				this.appendMaster();
				var that=this;
				setTimeout(function(){
					var Arr1=that.oldData.split('\n');
					that.deviceArrOld=[];
					for (var i = 0; i < Arr1.length; i++) {
						var item=Arr1[i];
						var itemArr=item.split('\t');
						if(itemArr[0]!=''){
							that.deviceArrOld.push({
								'name':itemArr[0],
								'count':itemArr[1]
							})
						}
						
					}
					//初始化
					for (var i = 0; i < that.deviceArr.length; i++) {
						var item=that.deviceArr[i];
						that.deviceArrNew.push({
							'name':item.name,
							'count':0
						})
					}
					
					//赋值
					for (var i = 0; i < that.deviceArrNew.length; i++) {
						var item=that.deviceArrNew[i];
						console.log(item.name,item.count)
						if(item.name=='DG-2024-0010')
						{
							console.log(1111);
						}
						for (var j = 0; j < that.deviceArrOld.length; j++) {
							var jitem=that.deviceArrOld[j];
							
							if(jitem.name.indexOf(item.name)>-1){
								that.deviceArrNew[i]=jitem;
								console.log(jitem);
							}
						}
					}
					console.log(that.deviceArrNew);
					that.newData='';
					
					var finsArr=that.groupAddArr(that.deviceArrNew);
					for (var i = 0; i < finsArr.length; i++) {
					   var item=finsArr[i];
					   var master=that.findMasterByPro(item[0]);
					   if(typeof item[0] !='undefined'){
						  that.newData+=item[0]+'\t'+item[1]+'\t'+master+'\n'; 
					   }else{
						   console.log(item);
					   }
					   
					}
				},2000);
				
			},
			//将设备按照工序汇总
			groupAddArr(devArr){
				var that=this;
				this.deviceGroupByArr=[];
				
				var tempDic={};//工序数据字典
				
				//第二个开始
				for (var i = 0; i < devArr.length; i++) {
				   var item=devArr[i];
				   
				   var pro=that.findProByDev2(item.name);
				   if(item.name.indexOf('DG-2023-0617 (40#)')>-1){
					   
					   console.log(pro,item);
				   }
				   console.log(pro,item.count)
				   //如果是新工序
				   if(typeof tempDic[pro]=='undefined'){
					tempDic[pro]=parseInt(item.count);
					//如果是旧工序
				   }else{
					   tempDic[pro]=parseInt(tempDic[pro])+parseInt(item.count);
				   }
				   
				}
				this.deviceGroupByArr = Object.keys(tempDic).map(key => [key, tempDic[key]]);
				return this.deviceGroupByArr;
			},
			//根据设备资产编号找到对应工序
			findProByDev(dNo){
				var pro='叠层制造部';
				for (var i = 0; i < this.deviceArr.length; i++) {
					var item=this.deviceArr[i];
					if(typeof dNo!='undefined'&&dNo.indexOf(item.name)>-1){
						pro=item.pro;
						break;
					}
				}
				return pro;
			},
			//根据设备资产编号找到对应工序
			findProByDev2(dNo){
				var pro='叠层制造部';
				for (var i = 0; i < this.deviceArr.length; i++) {
					var item=this.deviceArr[i];
					if(typeof dNo!='undefined'&&dNo.indexOf(item.name)>-1){
						pro=item.pro;
						break;
					}
				}
				return pro;
			},
			//根据工序找到对应的负责人
			findMasterByPro(pro){
				var master='负责人';
				for (var i = 0; i < this.deviceMasterArr.length; i++) {
					var item=this.deviceMasterArr[i];
					if(item.alias.indexOf(pro)>-1){
						master=item.master;
						break;
					}
				}
				return master;
			},
			appendDev() {
				if (this.deviceArr.length < 100) {
					this.loadTextFile('../../devices.text'); //加载人员数组
				}
			},
			
			loadTextFile(filePath) {
				fetch(filePath)
					.then(response => response.text())
					.then(text => {
						this.textContent = text;
						this.getarr();
					})
					.catch(error => {
						console.error('Error reading file:', error);
					});
			},
			getarr() {
				var arr = this.textContent.split("\n")
				for (var i = 0; i < arr.length; i++) {
					var line = arr[i];
					var arr2=line.split('	');
					this.deviceArr.push({
						'name':arr2[0],
						'pro':arr2[1],
						'count':0
					})
				}
				console.log(this.deviceArr);
			},
			appendMaster() {
				if (this.deviceMasterArr.length < 100) {
					this.loadTextFileMaster('../../master.txt'); //加载人员数组
				}
			},
			
			loadTextFileMaster(filePath) {
				fetch(filePath)
					.then(response => response.text())
					.then(text => {
						this.textContentMaster = text;
						this.getarrMaster();
					})
					.catch(error => {
						console.error('Error reading file:', error);
					});
			},
			getarrMaster() {
				var arr = this.textContentMaster.split("\n")
				for (var i = 0; i < arr.length; i++) {
					var line = arr[i];
					var arr2=line.split('\t');
					this.deviceMasterArr.push({
						'pro':arr2[0],
						'alias':arr2[1],
						'master':arr2[2].replace('\r','')
					})
				}
				console.log(this.deviceMasterArr);
			}
			
		}
	}
</script>

<style>
</style>